package okl.test.motionChart
{
	import flash.events.Event;
	
	import mx.binding.utils.BindingUtils;
	import mx.charts.BubbleChart;
	import mx.charts.chartClasses.ChartBase;
	import mx.collections.ArrayCollection;
	import mx.events.FlexEvent;
	
	import okl.test.motionChart.interfaces.IMotionChart;
	import okl.test.motionChart.parts.ColorScaleIndicator;
	import okl.test.motionChart.skins.MotionChartSkin;
	
	import spark.components.ComboBox;
	import spark.components.DropDownList;
	import spark.components.List;
	import spark.components.supportClasses.SkinnableComponent;
	
	/**
	 *  Dispatched when the <code>dataProvider</code> property changes.
	 *
	 *  @eventType mx.events.FlexEvent.DATA_CHANGE
	 */
	[Event(name="dataChange", type="mx.events.FlexEvent")]
	
	/**
	 * Dynamic chart to explore several data dimensions (object properties) over time.
	 * 
	 * <p>Inspired by google motion chart component used previously</p>
	 *	
	 * @author "oleksandr.kulinchenko@gmail.com"
	 */
	public class MotionChart extends SkinnableComponent implements IMotionChart
	{
		public function MotionChart()
		{
			super();
			setStyle("skinClass", MotionChartSkin);
		}
		
		private var _dataProvider:ArrayCollection, __dataProviderDirty:Boolean;
		
		[Bindable("dataChange")]
		[Inspectable(category="Data", defaultValue="undefined")]		
		/**
		 *  Set of data to be viewed 
		 * 
		 *  <p>
		 *  Please note that at the moment it accepts strongly typed data 
		 *  </p>
		 *  @default null
		 *  @see mx.collections.ArrayCollection
		 *  @see okl.test.motionChart.data.IMotionBubbleData
		 *  
		 */
		public function get dataProvider():ArrayCollection{return _dataProvider;}		

		public function set dataProvider(value:ArrayCollection/*IMotionBubbleData*/):void{
			if(dataProvider==value)return;
			_dataProvider=value;
			__dataProviderDirty=true;
			invalidateProperties();
			dispatchEvent(new Event(FlexEvent.DATA_CHANGE));
		}
		
		//{regiion LC
		override protected function commitProperties():void{
			super.commitProperties();
			if(__dataProviderDirty){
				//
				chartDescriptor = new BubbleChartDescriptor(chart as BubbleChart, listCats);
				chartDescriptor.chartData=  dataProvider;
				
				BindingUtils.bindProperty(chartDescriptor, "vProp", cbYAxisProp, ["selectedItem", "value"]);
				BindingUtils.bindProperty(chartDescriptor, "hProp", cbXAxisProp, ["selectedItem", "value"]);
				BindingUtils.bindProperty(chartDescriptor, "rProp", cbSizeAxisProp, ["selectedItem", "value"]);
				BindingUtils.bindProperty(chartDescriptor, "bubbleColorRules", cbColorAxisProp, ["selectedItem", "value"]);
				
				BindingUtils.bindProperty(chartDescriptor, "rProp", cbSizeAxisProp, ["selectedItem", "value"]);
				
				BindingUtils.bindProperty(chartDescriptor, "vScale", cbYLogAxisProp, ["selectedItem", "value"]);
				BindingUtils.bindProperty(chartDescriptor, "hScale", cbXLogAxisProp, ["selectedItem", "value"]);
				
				BindingUtils.bindProperty(cbXLogAxisProp, "visible", chartDescriptor, "hLinear");
				BindingUtils.bindProperty(cbXLogAxisProp, "includeInLayout", chartDescriptor, "hLinear");
		
				colorScaleIndicator.gradient = chartDescriptor.gcolors;
				
				__dataProviderDirty=false;
			}
		}		
		//}endregion
		
		//{region OLD_WAY
		[Bindable]private var chartDescriptor:BubbleChartDescriptor;
		//}endregion
		
		//{region SKIN_PARTS
		[SkinPart(required=true)]public var chart:ChartBase;
		[SkinPart(required=true)]public var listCats:List;		
		
		[SkinPart(required=false)]public var cbXAxisProp:ComboBox;
		[SkinPart(required=false)]public var cbYAxisProp:ComboBox;	
		[SkinPart(required=false)]public var cbSizeAxisProp:ComboBox;	
		[SkinPart(required=false)]public var cbDateUnits:ComboBox;
		[SkinPart(required=false)]public var cbColorAxisProp:ComboBox;	
		
		[SkinPart(required=false)]public var cbYLogAxisProp:ComboBox;
		[SkinPart(required=false)]public var cbXLogAxisProp:ComboBox;
		
		[SkinPart(required=false)]public var colorScaleIndicator:ColorScaleIndicator;	
		//filters
		
		
		//}endregion
	}
}